package com.twu.biblioteca;


import java.io.InputStream;
import java.util.Scanner;

public class Login {
    private InputStream libraryNumber;
    private InputStream password;
    private UserList userList;

    public Login(InputStream libraryNumber, InputStream password, UserList userList) {
        this.libraryNumber = libraryNumber;
        this.password = password;
        this.userList = userList;
    }

    public int requireLibraryNumber() {
        int libraryNumber;
        Scanner enteredLibraryNumber = new Scanner(this.libraryNumber);
        System.out.println("Please enter your Library Number: ");
        String temp = enteredLibraryNumber.nextLine();
        temp = temp.replace("-", "");
        libraryNumber = Integer.parseInt(temp);

        return libraryNumber;
    }

    public String requirePassword() {
        String password;
        Scanner enteredPassword = new Scanner(this.password);
        System.out.println("Please enter your Password: ");
        password = enteredPassword.nextLine();

        return password;
    }

    public User checkLogin() {
        User user = userList.findByLibraryNumber(requireLibraryNumber());
        if (user == null || !user.checkPassword(requirePassword())) {
            return null;
        }
        return user;
    }
}
